import { defineStore } from "pinia";

interface AppState {
  reloadFlag: boolean;
  sideBarCollapse: boolean;
}

export const useAppStateStore = defineStore({
  id: "app-app-state",
  state: (): AppState => ({
    reloadFlag: true,
    sideBarCollapse: false,
  }),
  actions: {
    /**
     * 重载页面
     * @param duration - 重载的延迟时间(ms)
     */
    async reloadPage(duration = 0) {
      this.reloadFlag = false;
      await nextTick();
      if (duration) {
        setTimeout(() => {
          this.reloadFlag = true;
        }, duration);
      } else {
        this.reloadFlag = true;
      }
      setTimeout(() => {
        document.documentElement.scrollTo({ left: 0, top: 0 });
      }, 100);
    },
    /**
     * 设置侧边栏的状态.
     * @param collapse
     */
    setSideBarCollapse(collapse: boolean) {
      this.sideBarCollapse = collapse;
    },
    /**
     * 改变侧边栏的折叠/展开状态.
     */
    toggleSideBarCollapse() {
      this.sideBarCollapse = !this.sideBarCollapse;
    },
  },
});
